package com.wyiot.core.exception;

import com.wyiot.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理方案
 *
 * @author yangw
 * @since V2.0.0
 */
@Slf4j
@ControllerAdvice(basePackages = "com.wyiot.pet")
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R jsonErrorHandler(HttpServletRequest req, Exception e) {
        if (e instanceof ValidateException) {
            return  R.error(e.getMessage());
        } else {
            log.error("Catch a exception in API[{}]", req.getRequestURL().toString(), e);
            return  R.error("系统错误");
        }
    }

}
